package com.wunu.smartlock1.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.adapter.AuthorizedRecordAdapter;
import com.wunu.smartlock1.model.AuthorizedRecordVo;
import com.wunu.smartlock1.net.response.LockEvent;
import com.wunu.smartlock1.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 开锁记录
 * Created by admin on 2018/4/21.
 */

public class LockRecordActivity_net_bak extends AppCompatActivity {
    public static boolean isChecked = true;
    private CheckBox authorized;
    private CheckBox tempoary;
    AuthorizedRecordAdapter adapter;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.smartlock_activity_lockrecord);
        tempoary = (CheckBox) findViewById(R.id.tempoary);
        authorized = (CheckBox) findViewById(R.id.authorized);

        String info = getIntent().getStringExtra("param");
        System.out.println("info :"+info);
        List<LockEvent> lists = CommonUtil.gson.fromJson(info,new TypeToken<List<LockEvent>>() {
        }.getType());

        System.out.println("list:"+ lists.size()+ lists.get(0));
        System.out.println("list:"+ lists.get(0).getEvent()+"\t"+lists.get(0).getId());
        final List<AuthorizedRecordVo> list = CommonUtil.transLockEventList(lists);
        System.out.println("list size :"+list);
        if(list == null || list.size() == 0){
            return;
        }
        //testdata
        AuthorizedRecordVo obj = new AuthorizedRecordVo();
        obj.setAuthorizedCode("903269");
        obj.setAuthorizedName("爱恨交戈");
        obj.setState(0);
        obj.setLockTime("2019-04-21T06:02:35.166Z");
        obj.setMemberType(1);
        list.add(obj);

        authorized.setChecked(isChecked);
        tempoary.setChecked(!isChecked);

        adapter = new AuthorizedRecordAdapter(LockRecordActivity_net_bak.this, R.layout.smartlock_activity_lockrecord_item, getMemberList(list,isChecked));
        ListView listView = (ListView) findViewById(R.id.recordListView);
        listView.setAdapter(adapter);

        authorized.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                isChecked = true;
                tempoary.setChecked(false);
                authorized.setChecked(true);

                adapter =new AuthorizedRecordAdapter(LockRecordActivity_net_bak.this, R.layout.smartlock_activity_lockrecord_item, getMemberList(list,isChecked));
                ListView listView = (ListView) findViewById(R.id.recordListView);
                listView.setAdapter(adapter);
            }
        });

        tempoary.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                isChecked = false;
                tempoary.setChecked(true);
                authorized.setChecked(false);

                adapter =new AuthorizedRecordAdapter(LockRecordActivity_net_bak.this, R.layout.smartlock_activity_lockrecord_item, getMemberList(list,isChecked));
                ListView listView = (ListView) findViewById(R.id.recordListView);
                listView.setAdapter(adapter);
            }
        });
}

    public List<AuthorizedRecordVo> getMemberList(List<AuthorizedRecordVo> alllist,boolean flag){
        List<AuthorizedRecordVo> list = new ArrayList<AuthorizedRecordVo>();
        for(int i=0;i<alllist.size();i++){
            if (((flag & alllist.get(i).isRoot() ) || !(flag | alllist.get(i).isRoot()))){
                list.add(alllist.get(i));
            }
        }
        return list;
    }
}
